package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.base.BasicAdapter;
import com.xywy.bloodandroid.util.UIUtil;

/**
 * *****************************************
 * 
 * @Title: SearchAct.java
 * @Package com.xywy.bloodandroid.act
 * @Description: TODO(搜索页面)
 * @author smz
 * @date 2014-12-26 上午10:00:05
 * @version V1.0
 ******************************************* 
 */
public class SearchAct extends BaseActivity {

	private RelativeLayout left_img;
	private EditText search_edit;
	private TextView confirm_tv;
	private ListView listview;

	private List<String> list;
	private SerachAdapter adapter;
	private SharedPreferences sp;

	@Override
	protected void initView(Bundle savedInstanceState) {
		setContentView(R.layout.act_search);
		left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText(R.string._search_tv);

		search_edit = (EditText) findViewById(R.id.search_edit);
		confirm_tv = (TextView) findViewById(R.id.confirm_tv);
		listview = (ListView) findViewById(R.id.listview);
		list = new ArrayList<String>();

		sp = context.getSharedPreferences("xywy_blood_info",
				Activity.MODE_PRIVATE);
		adapter = new SerachAdapter(context, list);
		listview.setAdapter(adapter);
		// for (int i = 0; i < 4; i++) {
		// list.add("111");
		// }
		HashSet<String> set = (HashSet<String>) sp.getStringSet("lsit", null);
		// list.addAll();
		if (set != null) {
			Iterator<String> iterator = set.iterator();
			while (iterator.hasNext()) {
				list.add(iterator.next());
				adapter.notifyDataSetChanged();
			}
		}
		
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		confirm_tv.setOnClickListener(this);
	}

	@Override
	protected void initData() {

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			Finish("");
			break;
		case R.id.confirm_tv:
			String value = search_edit.getText().toString().trim();
			list.add(value);
			if (value.length() > 0) {
				Set<String> set = new HashSet<String>();
				set.addAll(list);// 给set填充
				sp.edit().putStringSet("lsit", set).commit();

				Finish(value);
			} else {
				UIUtil.showToast(context, "关键字不能为空~");
				
			}	
			
			break;

		default:
			break;
		}
	}
	
	private void  Finish(String value){
		Intent date = new Intent();
		date.putExtra("serach_act", value);
		setResult(2, date);
		SearchAct.this.finish();
	};

	public class SerachAdapter extends BasicAdapter<String> {
		private LayoutInflater inflater;

		public SerachAdapter(Context context, List<String> list) {
			super(context, list);
			this.inflater = LayoutInflater.from(context);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			viewholder holder;
			if (convertView == null) {
				holder = new viewholder();
				convertView = inflater.inflate(R.layout.item_search, null);
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_name);
				holder.tv_name.setText(list.get(position));
				convertView.setTag(holder);
			} else {
				holder = (viewholder) convertView.getTag();
			}
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					search_edit.setText(list.get(position));
				}
			});
			return convertView;
		}

		private class viewholder {
			private TextView tv_name;
			private TextView xian;
		}
	}
}
